#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#define BUFSIZE 1000

int main(int argc, char **argv)
{
	int fp;
	void fileCopy(int, int);
	char *prog = argv[0];

	if(argc == 1)
		fileCopy(0, 1);
	else 
		while(--argc > 0)
			if((fp = open(*++argv, O_RDONLY, 0)) == -1)
			{
				fprintf(stderr, "%s: can't open %s\n", prog, *argv);
				return 1;
			}
			else
			{
				fileCopy(fp, 1);
				close(fp);
			}
	return 0;
}

void fileCopy(int ifp, int ofp)
{
	int n;
	char buf[BUFSIZE];

	while((n = read(ifp, buf, BUFSIZE)) > 0)
		if(write(ofp, buf, n) != n)
			fprintf(stderr, "error: write error\n");
}
